package tankgame;

import javax.swing.*;
import javax.xml.bind.ValidationException;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @description:
 * @Author yyp
 * @time 2022/3/28
 */
public class MyPanel extends JPanel  implements KeyListener {
    //int x = 100;
    //int y = 100;
    Hero hero = null;

    Vector<EnemyTank> enemyTanks = new Vector<>();

    Vector<Bullet> heroBullets = new Vector<>();


    int enemyTankSize = 3;

    public MyPanel(){
        hero = new Hero(100,100);

        for (int i = 0; i < enemyTankSize; i++) {
            //创建一个敌人的坦克
            EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
            //设置方向
            enemyTank.setDirect(2);
            //加入
            enemyTanks.add(enemyTank);
        }
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.fillRect(0,0,1000,750);

        Draw.drawTank(hero.getX(),hero.getY(),g, hero.getDirect(), 1);

        //画出敌人的坦克, 遍历 Vector
        for (int i = 0; i < enemyTanks.size(); i++) {
            //取出坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            Draw.drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
        }
        for (int i = 0; i < heroBullets.size(); i++) {
            Bullet bullet = heroBullets.get(i);
            if ((bullet.getX()<0)||(bullet.getY()<0)||(bullet.getX()>1000)||(bullet.getY()>750)) {
                heroBullets.remove(i);
            }
            Draw.drawBullet(bullet.getX(),bullet.getY(),bullet.getDirect(),bullet.getSpeed(),g);
        }
        for (Bullet bullet : heroBullets) {
            switch (bullet.getDirect()){
                case 0:
                    bullet.moveUp();
                    break;
                case 1:
                    bullet.moveRight();
                    break;
                case 2:
                    bullet.moveDown();
                    break;
                case 3:
                    bullet.moveLeft();
                    break;
            }
        }
        try {
            Thread.sleep(50);
            this.repaint();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //System.out.println((char)e.getKeyCode() + "被按下..");
        // 根据用户按下的不同键，来处理小球的移动 (上下左右的键)
        // 在 java 中，会给每一个键，分配一个值(int)
        if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            //KeyEvent.VK_DOWN 就是向下的箭头对应的 code
            hero.moveDown();
            hero.setDirect(2);
        } else if(e.getKeyCode() == KeyEvent.VK_UP) {
            hero.moveUp();
            hero.setDirect(0);
        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            hero.moveLeft();
            hero.setDirect(3);
        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            hero.moveRight();
            hero.setDirect(1);
        }//让面板重绘
        else if(e.getKeyCode() == KeyEvent.VK_J) {
            heroBullets.add(new Bullet(hero.getX(),hero.getY(),hero.getDirect()));
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}
